#!/bin/bash
# 李广龙
#选择nginx 版本自动安装
#使用该脚本请保持联网环境


echo "提示:脚本会关闭 firewalld 防火墙,并且自动配置 开机自启,和 全局变量"
echo "适用系统版本:Centos 7"

echo "当前系统版本:"
cat /etc/redhat-release

Nginx="nginx-1.20.2.tar.gz"
Dir="/usr/local/"

echo "Nginx默认为1.20.2版本，默认安装路径为:/usr/local/"
#-------爬取nginx官网页面--------------
# yum -y install lynx &>/dev/null
#抓取页面
# lynx -dump http://nginx.org/en/download.html | awk '{print $2 }'   |  egrep -o "(nginx-)(.*)(.tar.gz)" | awk '!x[$0]++' |cat -n >/tmp/Version.txt
# Version=`cat /tmp/Version.txt`
#使用一下命令抓取下载链接
#lynx -dump http://nginx.org/en/download.html | sed -n '/nginx-.*.tar.gz/'p a.html | egrep -o "(http)(.*)(.tar.gz)" |  awk '!x[$0]++' |cat -n
# 有的版本下载不下载,有问题,忽略这种方式



TIME=[$(date +'%Y-%m-%d %H:%M:%S')]

#写入自定义配置文件
writeInConfig(){
cat << EOF >$Dir/nginx/conf/nginx.conf
user root;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
  	server_tokens off;
  	client_max_body_size 100m;
	  charset 'utf-8';

    server {
        listen       80;
        server_name  localhost;


        location /api/ {
            proxy_pass http://127.0.0.1:8000/user/;
        }

        # -------------------------
        location /html {
            alias /data/htmlfile/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
EOF
}

#定义安装函数体
Install_Nginx(){
#-------关闭防火墙seliunx--------------
systemctl  stop  firewalld
systemctl disable firewalld
setenforce 0
#--------------------------------------
echo "${TIME}正在安装依赖包..................."
yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y

echo "${TIME}正在下载Nginx软件包..................."
wget http://nginx.org/download/$Nginx

echo "${TIME}正在解压软件包..................."
tar -zxvf $Nginx
rm -rf $Nginx

echo "${TIME}正在安装Nginx..................."

nginx_dir=`ls | grep -v ".tar.gz"$ | grep nginx |awk 'NR==1'`

cd  $nginx_dir/ || exit

./configure --with-http_ssl_module --with-stream

make
make install
ln -s  ${Dir}nginx/sbin/nginx   /usr/bin/nginx

echo "${TIME}写入自定义config文件..................."
writeInConfig

echo "${TIME}Nginx安装完成..................."


}
Install_Nginx




#定义加入系统服务函数体
Nginx_service(){
cat << EOF >/usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=${Dir}nginx/sbin/nginx
ExecReload=${Dir}nginx/sbin/nginx -s reload
ExecStop=${Dir}nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl  daemon-reload
}

#--------------------------判断是否将nginx加入系统服务------
read  -p "是否加入系统服务,默认不加入（y/n）："  Ssm_service

if   [ ! -n "$Ssm_service" ] || [ "$Ssm_service" != "y" ] || [ "$Ssm_service" != "Y" ] || [ "$Ssm_service" != "n" ] || [ "$Ssm_service" != "N" ]
	then
		if [ "$Ssm_service" = "y" ] || [ "$Ssm_service" = "Y" ];
			then
				Nginx_service
				echo "${TIME}Nginx 已加入系统服务"
		elif [ "$Ssm_service" = "n" ] || [ "$Ssm_service" = "N" ];
			then
				echo "${TIME}${Dir}nginx/sbin/nginx  为启动文件"
		fi
fi






#--------------------------判断是否启动nginx------
read  -p "是否启动Nginx,和开机自启（y/n）："  start_no

if [ "$Ssm_service" = "y" ] || [ "$Ssm_service" = "Y" ];
	then
			if [ "$start_no" = "y" ] || [ "$start_no" = "Y" ];
				then
					systemctl  start nginx
					systemctl enable nginx
					 if [ $? -eq 0 ]
						then
							echo "${TIME}已经使用 systemctl  start nginx 启动"
						else
							echo "${TIME}nginx 启动错误，排查是否端口冲突&&其他错误"
					 fi
			elif [ "$start_no" = "n" ] || [ "$start_no" = "N" ];
				then
					echo "${TIME}nginx没有启动"
			elif   [ ! -n "$start_no" ] || [ "$start_no" != "y" ] || [ "$start_no" != "Y" ] || [ "$start_no" != "n" ] || [ "$start_no" != "N" ]
				then
					echo "${TIME}请输入正确的值（y/n）"
			fi
elif  [ "$Ssm_service" = "n" ] || [ "$Ssm_service" = "N" ];
	then
			if [ "$start_no" = "y" ] || [ "$start_no" = "Y" ];
				then
					${Dir}nginx/sbin/nginx
					 if [ $? -eq 0 ]
						then
							echo  "${TIME}已经使用${Dir}nginx/sbin/nginx启动"
						else
							echo "${TIME}nginx 启动错误，排查是否端口冲突&&其他错误"
					 fi
			elif [ "$start_no" = "n" ] || [ "$start_no" = "N" ];
				then
					echo "${TIME}nginx没有启动"
			elif   [ ! -n "$start_no" ] || [ "$start_no" != "y" ] || [ "$start_no" != "Y" ] || [ "$start_no" != "n" ] || [ "$start_no" != "N" ]
				then
					echo "${TIME}请输入正确的值（y/n）"
			fi
fi

#---------------------------------
